// 组件导航
const compNav = (obj,type='jump-main')=>{
  if(type=='jump-main'){
    obj.jump=(compName,data)=>{
        obj.$root.$refs.main.chgComp(compName, data)
    }
  }
  else if(type=='jump'){
    obj.jump=(compName,data)=>{
        obj.$root.chgComp(compName, data)
    }
  }
  else if(type =='nav'){
      obj.chgComp=(compName, data={})=>{
        // pushComp
        if(compName == obj.currentView){
          obj.currentView =''
          // TODO 强制刷新有点low
          setTimeout(()=>{
            obj.replaceComp(compName, data)
          })
        } else{
          obj.pushComp(compName, data)
        }
      }
      obj.pushComp=(compName, data={})=>{
        if(obj.history.length -1 != obj.currentIndex){
          obj.history.splice(obj.currentIndex+1)
        }
        obj.history.push({compName, data})
        obj.currentView = compName
        obj.currentIndex = obj.history.length-1
        obj.data= data
      }
      obj.go=(n=0)=>{
        let len = obj.history.length
        let newIndex = obj.currentIndex+n
        if(newIndex<len && newIndex>=0){
          obj.currentView = obj.history[newIndex].compName
          obj.currentIndex = newIndex
        }
      }
      obj.back=()=>{
        obj.go(-1)
      }
      obj.forword=()=>{
        obj.go(1)
      }
      obj.replaceComp=(compName, data={})=>{
        obj.history[obj.currentIndex] = {compName, data}
        obj.currentView = compName
        obj.data= data
        obj.data.__random =Math.random()
      }
  }

}
export {compNav} 